/*
 *所有关于Luxs_supp_mara_detail类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Luxs_supp_mara_detailService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyErp.IRepositories;
using VOL.Core.DBManager;
using System.Collections.Generic;

namespace MyErp.Services
{
    public partial class Luxs_supp_mara_detailService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ILuxs_supp_mara_detailRepository _repository;//访问数据库
        WebResponseContent webResponse = new WebResponseContent();
        [ActivatorUtilitiesConstructor]
        public Luxs_supp_mara_detailService(
            ILuxs_supp_mara_detailRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            AddOnExecuting = (Luxs_supp_mara_detail mara_d, object list) =>
            {
                List<Luxs_supp_mara_detail> ls = DBServerProvider.DbContext.Set<Luxs_supp_mara_detail>().Where(w => w.matnr == mara_d.matnr && w.supper_id== mara_d.supper_id).ToList();
                if (ls.Count >= 1)
                {
                    return webResponse.Error("此物料对应的供应商已经存在！");
                }
                return webResponse.OK();
            };
          
                return base.Add(saveDataModel);
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecuted = (Luxs_supp_mara_detail mara_d, object addList, object updateList, List<object> delKeys) =>//防止重复的逻辑写在UpdateOnExecuted中表示提交到数据库中，否则修改其他字段时有矛盾，然后回滚
            {
                List<Luxs_supp_mara_detail> ls = DBServerProvider.DbContext.Set<Luxs_supp_mara_detail>().Where(w => w.matnr == mara_d.matnr && w.supper_id == mara_d.supper_id).ToList();
                if (ls.Count >= 2)
                {
                    return webResponse.Error("此物料对应的供应商已经存在！");
                }
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }

    }
}
